package lib.register;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lib.register.registerDao;

/**
 * Servlet implementation class registerServlet
 */
@WebServlet("/registerServlet")
public class registerServlet implements javax.servlet.Servlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws Exception  {
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");// 取得用户名
		String uid = request.getParameter("uid");// 取得卡号
		String status[] = request.getParameterValues("ustatus");// 取得身份
		String password = request.getParameter("password");// 取得密码
		
		registerDao db = new registerDao(); // 构建登陆对象
		boolean canRegister = db.insert(username,uid,status[0], password);// 取得用户名和密码
		if (canRegister) {// 根据登陆情况，跳转页面
			System.out.println("注册成功");
			response.sendRedirect("index.jsp");
		} else {
			response.sendRedirect("register_wrong.jsp");
			System.out.println("注册失败");
		}
	}

	public registerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}
	
	public void destroy() {
		// TODO Auto-generated method stub
	}
	
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null; 
	}
	
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpServletRequest rq = (HttpServletRequest) request;
		HttpServletResponse rs = (HttpServletResponse) response;
		try {
			doPost(rq, rs);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
